<?php

/*
Copyright 2005-2011 Textpattern CMS Project
All Rights Reserved.

This file is part of Textpattern.

Textpattern is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('txp'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

//------------------------------------------------------------------------------

class _StylesModel extends SparkModel
{
	private $columns;

	//---------------------------------------------------------------------------

	public function __construct($params)
	{
		parent::__construct($params);
		$this->columns = array_flip(array('name', 'css',));
	}

	/**
	 * Retrieve the stylesheet name in use by your default Textpattern Section.
	 * 
	 * @return string Stylesheet name
	 */
	public function defaultStyle()
	{
		$db = $this->loadDB();

		$style_row = $db->selectRow('txp_section', 'css', "name='default'");
		$style = $style_row['css'];

		$this->validateStyle($style);

		return $style;
	}

	//---------------------------------------------------------------------------
	
	public function newStyle($style)
	{
		$this->filterStyle($style);
		$this->validateStyle($style);

		return $style;
	}

	//---------------------------------------------------------------------------
	
	public function addStyle(&$style)
	{
		$db = $this->loadDB();
		
		$this->filterStyle($style);
		$this->validateStyle($style);

		$db->insertRow('txp_css', $style);
	}

	//---------------------------------------------------------------------------
	
	public function updateStyle($style)
	{
		$db = $this->loadDB();

		$this->filterStyle($style);
		$this->validateStyle($style);

		$name = $style['name'];
		unset($style['name']);

		$db->updateRows('txp_css', $style, 'name=?', $name);
	}

	//---------------------------------------------------------------------------
	
	public function deleteStyle($style)
	{
		$db = $this->loadDB();

		$this->validateStyle($style);

		$db->deleteRows('txp_css', 'name=?', $style['name']);
	}

	//---------------------------------------------------------------------------
	
	public function findStyleByName($name, $columns = '*')
	{
		$db = $this->loadDB();

		if (!$row = $db->selectRow('txp_css', $columns, 'name=?', $name))
		{
			return false;
		}
		
		return $row;
	}

	//---------------------------------------------------------------------------
	
	public function findStyles($filters = NULL, $columns = '*')
	{
		$db = $this->loadDB();

		$where = $db->buildConjunction('txp_css', $filters, $bind);

		return $db->query($db->buildSelect('txp_css', $columns, NULL, $where, 'name'), $bind)->rows();
	}

	//---------------------------------------------------------------------------
	
	private function filterStyle(&$style)
	{
		$style = array_intersect_key($style, $this->columns);
	}

	//---------------------------------------------------------------------------
	
	private function validateStyle(&$style)
	{
		if (empty($style['name']))
		{
			throw new SparkHTTPException_BadRequest();
		}
	}

//------------------------------------------------------------------------------
	
}
